#include <iostream>

#if 0
template <typename T>
void Swap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

struct Student
{
    int id;
    std::string name;
};

int main()
{
    Student s1 = {1, "zhangsan"};
    Student s2 = {2, "lisi"};

    Swap(s1, s2);
    std::cout << s1.id << " " << s1.name << std::endl;
    std::cout << s2.id << " " << s2.name << std::endl;
    return 0;
}
#endif

#if 0
// 具体问题具体分析（只换学号）
template <typename T>
void Swap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

struct Student
{
    int id;
    std::string name;
};

// 模板具体化：为模板中抽象的泛型指定具体的类型
// 具体问题具体分析
template <>
void Swap<Student>(Student &s1, Student &s2)
{
    int temp = s1.id;
    s1.id = s2.id;
    s2.id = temp;
}

int main()
{
    Student s1 = {1, "zhangsan"};
    Student s2 = {2, "lisi"};

    Swap(s1, s2);
    std::cout << s1.id << " " << s1.name << std::endl;
    std::cout << s2.id << " " << s2.name << std::endl;
    return 0;
}
#endif

#if 1
template <typename T>
void Swap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

struct Student
{
    int id;
    std::string name;
    bool operator>(const Student &s)
    {
        return id > s.id;
    }
};

template <typename T>
void Sort(T *a, int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = 0; j < size - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                // 在模板函数中调用模板函数，类型仍然是不确定的，所以显式调用还是T类型
                Swap<T>(a[j], a[j + 1]);
            }
        }
    }
}

int main()
{
    Student s[2] = {
        {1, "zhangsan"},
        {2, "lisi"}};
    Sort(s, 2);
    std::cout << s->id << std::endl;
    return 0;
}
#endif